package com.sdy.dcsb.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.sdy.common.model.Response;
import com.sdy.dcsb.biz.model.EsbLogJoint;
import com.sdy.dcsb.biz.service.EsbLogService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import static org.springframework.util.StringUtils.isEmpty;

/**
 * <p>
 * 服务日志表 前端控制器
 * </p>
 *
 * @author fb
 * @since 2019-10-11
 */
@Slf4j
@Controller
@RequestMapping("/EsbLog")
@Api(tags = "服务日志相关接口")
public class EsbLogController extends BaseController {
    @Autowired
    private EsbLogService esbLogService;

    /**
     * 所有服务调用日志，分页
     * @param map json形式
     * @return
     */
    @PostMapping("/selectAll")
    @ResponseBody
    @ApiOperation("所有服务调用日志，分页")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "requestTimeBegin", value = "请求时间头，例：2019-10-11 15:30:46"),
            @ApiImplicitParam(dataType = "String", name = "requestTimeEnd", value = "请求时间尾，例：2019-10-11 16:33:51"),
            @ApiImplicitParam(dataType = "String", name = "type", value = "查询类别(默认展示服务名称)：1:服务名称,2:请求流水号,3:请求报文"),
            @ApiImplicitParam(dataType = "String", name = "message", value = "查询值"),
            @ApiImplicitParam(dataType = "String", name = "status", value = "status ,请求状态，0：成功，1：权限异常，2：服务异常，3：其他异常，4：流控异常"),
            @ApiImplicitParam(dataType = "String", name = "systemName", value = "消费者系统名称"),
            @ApiImplicitParam(dataType = "String", name = "current", value = "当前页"),
            @ApiImplicitParam(dataType = "String", name = "size", value = "每页数量"),
            @ApiImplicitParam(dataType = "Map<String,Object>", name = "map", value = "{\n" +
                    "\t\"requestTimeBegin\": \"2019-10-11 15:30:46\",\n" +
                    "\t\"requestTimeEnd\": \"2019-10-11 16:33:51\",\n" +
                    "\t\"type\": \"1\",\n" +
                    "\t\"message\": \"\",\n" +
                    "\t\"status\": \"\",\n" +
                    "\t\"systemName\": \"\",\n" +
                    "\t\"current\": 1,\n" +
                    "\t\"size\": 10\n" +
                    "}"),
    })
    public Response selectAll(@RequestBody Map<String,Object> map) {

        String requestTimeBegin = "";
        String requestTimeEnd = "";
        String type = "";
        String message = "";
        String status = "";
        String systemName = "";
        Integer current = 1;
        Integer size = 10;

        if(map.containsKey("requestTimeBegin") && map.containsKey("requestTimeEnd") && map.containsKey("type") &&
                map.containsKey("message") && map.containsKey("status") && map.containsKey("systemName")) {
            requestTimeBegin = (String) map.get("requestTimeBegin");
            requestTimeEnd = (String) map.get("requestTimeEnd");
            type = (String) map.get("type");
            message = (String) map.get("message");
            status = (String) map.get("status");
            systemName = (String) map.get("systemName");
        }else {
            return Response.error("查询参数不能为空");
        }

        if(map.containsKey("current")) {
            current = (Integer) map.get("current");
        }
        if(map.containsKey("size")) {
            size = (Integer) map.get("size");
        }

        JSONObject object = new JSONObject();

        object.put("size",size);
        object.put("current",current);
        object.put("searchCount",true);

        Integer total = 0;
        Integer page = 0;

        if(!isEmpty(requestTimeBegin) && !isEmpty(requestTimeEnd)) {
            if(!isEmpty(type)) {
                if(type.equals("3")) {
                    List<EsbLogJoint> resultCount = null;
                    List<EsbLogJoint> result = null;
                    // 处理clob字段所有情况的查询结果
                    if(isEmpty(message)) {
                        resultCount = esbLogService.findServiceLogListCount(requestTimeBegin, requestTimeEnd,
                                "servname", message, status, systemName);
                        result = esbLogService.findServiceLogList(requestTimeBegin, requestTimeEnd,
                                "servname", message, status, systemName, current, size);
                    }else {
                        resultCount = esbLogService.findServiceLogListCount(requestTimeBegin, requestTimeEnd,
                                "insoapmsg", message, status, systemName);
                        result = esbLogService.findServiceLogList(requestTimeBegin, requestTimeEnd,
                                "insoapmsg", message, status, systemName, current, size);
                    }
                    total = resultCount.size();
                    if(total%size==0){
                        page = total/size;
                    }else {
                        page = total/size+1;
                    }
                    object.put("records",result);
                    object.put("total",total);
                    object.put("pages",page);
                }else if(type.equals("2")) {
                    List<EsbLogJoint> resultCount = esbLogService.findServiceLogListCount(requestTimeBegin, requestTimeEnd,
                            "t1.reqid", message, status, systemName);
                    total = resultCount.size();
                    if(total%size==0){
                        page = total/size;
                    }else {
                        page = total/size+1;
                    }
                    List<EsbLogJoint> result = esbLogService.findServiceLogList(requestTimeBegin, requestTimeEnd,
                            "t1.reqid", message, status, systemName, current, size);
                    object.put("records",result);
                    object.put("total",total);
                    object.put("pages",page);
                }else {
                    List<EsbLogJoint> resultCount = esbLogService.findServiceLogListCount(requestTimeBegin, requestTimeEnd,
                            "servname", message, status, systemName);
                    total = resultCount.size();
                    if(total%size==0){
                        page = total/size;
                    }else {
                        page = total/size+1;
                    }
                    List<EsbLogJoint> result = esbLogService.findServiceLogList(requestTimeBegin, requestTimeEnd,
                            "servname", message, status, systemName, current, size);
                    object.put("records",result);
                    object.put("total",total);
                    object.put("pages",page);
                }
                return Response.success(object);
            }else {
                return Response.error("查询类别不能为空");
            }
        }else {
            return Response.error("请输入时间范围");
        }

    }

}



